<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\api\controller\v1\car;


use app\Request;
use app\services\car\CarOrderServices;
use app\services\car\CarTypeServices;
use think\facade\Db;
use app\services\car\CarInfoServices;
class CarTypeController
{
    /**
     * 获取门店车辆
     * @param Request $request
     * @param CarTypeServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCityTypeList(Request $request, CarTypeServices $services)
    {
        $where = $request->getMore([
            ['city_id', ''],//城市id
            ['place_id',''],//店铺id
            ['cate_id', ''],
            ['price_sort', 0],
            ['brand_id', ''],
            ['seat_sum', ''],
            ['gear', ''],
            ['min_price', ''],
            ['max_price', ''],
            ['stat_stamp', ''],  // 取车时间
            ['end_stamp', ''],   // 还车时间
            ['day', 0],          // 用车天数
            ['hour', 0],         // 用车小时数
            ['take_latitude', ''],
            ['take_longitude', ''],
            ['return_latitude', ''],
            ['return_longitude', ''],
        ]);
        if (!$where['city_id']) return app('json')->fail('城市参数错误');
        if (!$where['place_id']) return app('json')->fail('店铺参数错误');
        $where['place_id'];
        if($where['brand_id']){
            $where['brand_id'] = explode(',',$where['brand_id']);
        }else{
            unset($where['brand_id']);
        }

        return app('json')->success($services->getCityTypeList($where));
    }

    /**
     * 车型详情
     * @param Request $request
     * @param CarTypeServices $services
     * @return mixed
     */
    public function getCarTypeInfo(Request $request, CarTypeServices $services)
    {
        $where = $request->getMore([
            ['car_type_id', ''],
        ]);
        if (!$where['car_type_id']) return app('json')->fail('参数错误');
        return app('json')->success($services->getCarInfo($where));

    }

    /**
     * 车型详情2
     * @param Request $request
     * @param CarTypeServices $services
     * @return mixed
     */
    public function getCarTypeInfo2(Request $request, CarTypeServices $services)
    {
        $where = $request->getMore([
            ['car_type_id', ''],
            ['place_id','']
        ]);
        if (!$where['car_type_id']) return app('json')->fail('参数错误');
        if (!$where['place_id']) return app('json')->fail('店铺错误');
        return app('json')->success($services->getCarInfo2($where));

    }

    /**
     * 車輛纯租金
     * @param Request $request
     * @param CarOrderServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function CareFee(Request $request, CarOrderServices $services)
    {
        $where = $request->getMore([
            ['store_id', 0],         //下单门店
            ['take_place_id', 0],     //取车服务点
            ['return_place_id', 0],   //还车服务点
            ['is_other_places', false],  //是否异店还车
            ['type_id', 0],
            ['start_time', 0],
            ['end_time', 0],
            ['is_visit', false],
            ['take_latitude', ''],
            ['take_longitude', ''],
            ['return_latitude', ''],
            ['return_longitude', ''],
        ]);
        //车辆纯租金费+手续费、车辆整备费
        $result = $services->getCareFee($where);
        return app('json')->success($result);
    }

    /**
     * 获取车型保险费
     * @param Request $request
     * @param CarOrderServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getcarService(Request $request, CarOrderServices $services)
    {
        $where = $request->getMore([
            ['car_type_id', 0],
            ['start_time', 0],
            ['end_time', 0]
        ]);
        $data = $services->getcarService($where['car_type_id'], $where['start_time'], $where['end_time']);
        return app('json')->success($data);
    }


    //根据车型获取该店铺车型下面的所有车辆信息
    public function get_city_type_car_info(Request $request){ 
        $where = $request->getMore([
            ['car_type_id', ''],
            ['stat_stamp', ''],
            ['end_stamp', ''],
        ]);
        if (!$where['car_type_id']||!$where['stat_stamp']||!$where['end_stamp']) return app('json')->fail('参数错误');
        $carInfoServices = app()->make(CarInfoServices::class);
        $car_info = $carInfoServices->getCarInventory($where['car_type_id'],$where['stat_stamp'],$where['end_stamp']);
        $car_list = $car_info['car_list'];
//        if ($car_info['inventory']<=0){
//            throw new ValidateException('当前选择车辆已无库存，请选择其他车型下单！');
////            return false;
//        }
//        $car_info = Db::name('car_info')
//            ->alias('c')
//            ->field('c.id,c.car_no,c.img')
//            ->where('c.type_id',$where['car_type_id'])
//            ->where('c.car_status',1)
//            ->where('c.sxj_status',1)
//            ->select()->toArray();
        $car_type = Db::name('car_type')->where('id',$where['car_type_id'])->find();
        $type_info_pic =Db::name('car_type_info')->where('id',$car_type['type_info_id'])->value('pic');
        foreach($car_list as $k=>$v){
            if($v['img']){
                $car_list[$k]['img'] = json_decode($v['img'])[0];
            }else{
                $car_list[$k]['img'] = $type_info_pic;
            }
        }
        return app('json')->success($car_list);
    }

    //传车辆id，拿轮播图
    public function get_car_img(Request $request){
        $where = $request->getMore([
            ['car_id', ''],
        ]);
        if (!$where['car_id']) return app('json')->fail('参数错误');
        $car_info = Db::name('car_info')
            ->alias('c')
            ->field('c.img,c.type_id')
            ->where('c.id',$where['car_id'])
            ->find();
        if($car_info['img']){
            $car_info['img'] = json_decode($car_info['img']);
        }else{
            $car_type = Db::name('car_type')->where('id',$car_info['type_id'])->find();
            $type_info_pic =Db::name('car_type_info')->where('id',$car_type['type_info_id'])->value('pic');
            $car_info['img'] =[$type_info_pic];
        }
        return app('json')->success($car_info);
    }

}
